package com.wangijun.yuan.admin.system.service;

import cn.dev33.satoken.stp.StpUtil;
import com.wangijun.yuan.admin.base.CacheKey;
import com.wangijun.yuan.admin.base.CommonResult;
import com.wangijun.yuan.admin.base.YuanAdminConst;
import com.wangijun.yuan.admin.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

@Slf4j

@Service
public class IndexService {
    @Resource
    private SysUserService userService;


    /**
     * 功能描述: 修改密码
     *
     * @author wlf 2023/9/13 10:36
     */
    public void userPass(String pass1, String pass2, String pass3) {
        SysUser sysUser = userService.getLoginUser();
        if (!YuanAdminConst.md5.digestHex16(pass1).equals(sysUser.getPssswd())) {
            CommonResult.error("原始密码输入错误，修改失败");
        }
        if (!pass2.equals(pass3)) {
            CommonResult.error("两次密码输入不一致，请重新输入");
        }
        boolean update = userService.lambdaUpdate()
                .set(SysUser::getPssswd, YuanAdminConst.md5.digestHex16(pass2))
                .eq(SysUser::getId, sysUser.getId()).update();
        if (update) {
            // 修改密码过后退出登录
            StpUtil.logout(sysUser.getId(), YuanAdminConst.LoginDevice.PC.name());
        }
    }


    /**
     * 功能描述: 清理缓存方法
     *
     * @author wlf 2023/9/13 14:07
     */
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = CacheKey.role, allEntries = true),
                    @CacheEvict(cacheNames = CacheKey.menu, allEntries = true),
                    @CacheEvict(cacheNames = CacheKey.user, allEntries = true),
                    @CacheEvict(cacheNames = CacheKey.organ, allEntries = true),
                    @CacheEvict(cacheNames = CacheKey.power, allEntries = true),
            }
    )
    public void cleanCache() {

    }
}
